package kata;

public class UrlRewriter {
	String rewrite(String url) {
		String newUrl = "";
		String[] splitUrl = url.split("/");

		for (int i = 1; i < splitUrl.length; i++) {
			if (splitUrl[i].equals("article")) {
				i++;
				splitUrl[i] = "article?id=" + splitUrl[i];
			}
			if (splitUrl[i].equals("2008")) {
				splitUrl[i] = "2009";
			}
			if (splitUrl[i].equals("company") && splitUrl.length >= i + 3) {
				i += 2;
				splitUrl[i] = "company?country=" + splitUrl[i - 1] + "&city="
						+ splitUrl[i];
			}
			if (splitUrl[i].equals("company") && splitUrl.length < i + 3) {
				i += 1;
				splitUrl[i] = "company?country=" + splitUrl[i];
			}
			newUrl += "/" + splitUrl[i];
		}
		if (url.charAt(url.length() - 1) == '/') {
			newUrl += "/";
		}

		return newUrl;
	}
}
